package models

import (
	"gorm.io/gorm"
	"shop-microservices/basic/inits"
	"time"
)

type Brand struct {
	Id               uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	BrandName        string         `gorm:"column:brand_name;type:varchar(255);comment:品牌名称;not null;" json:"brand_name"`               // 品牌名称
	BrandLogo        string         `gorm:"column:brand_logo;type:varchar(255);comment:品牌logo;not null;" json:"brand_logo"`             // 品牌logo
	BrandDescription string         `gorm:"column:brand_description;type:varchar(255);comment:品牌简介;not null;" json:"brand_description"` // 品牌简介
	CreatedAt        time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt        time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt        gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

// TableName 获取表
func (b *Brand) TableName() string {
	return "brand"
}

// GoodsList 定义要查询的字段
type GoodsList struct {
	Id               uint32  `json:"id"`                //商品Id
	BrandLogo        string  `json:"brand_logo"`        //品牌logo
	GoodsName        string  `json:"goods_name"`        //商品名称
	BrandDescription string  `json:"brand_description"` //品牌简介
	GoodsPrice       float64 `json:"goods_price"`       //商品价格
	BrandName        string  `json:"brand_name"`        //品牌名称
	GoodsUrl         string  `json:"goods_url"`         //商品地址
	GoodsDesc        string  `json:"goods_desc"`        //商品简介
	GoodsStock       uint32  `json:"goods_stock"`       //商品库存
	GoodsStatus      uint8   `json:"goods_status"`      //商品状态
	Sales            int32   `json:"sales"`             //销量
}

// WelcomeSearchShopList 首页搜索商品列表
func (b *Brand) WelcomeSearchShopList(brandName string, page, pageSize int) (list []GoodsList, err error) {
	inits.DB.Model(&Brand{}).Select("goods.*,brand.brand_name as brand_name,brand.brand_logo as brand_logo,brand.brand_description as brand_description").
		Joins("join goods on goods.brand_name = brand.brand_name").
		Order("goods.sales desc").
		Scopes(WithBrandNameLike(brandName), Paginate(page, pageSize)).
		Find(&list)
	return
}

// WithBrandNameLike 品牌名称模糊查询
func WithBrandNameLike(name string) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if name == "" {
			return db
		}
		return db.Where("goods.brand_name LIKE CONCAT('%',?,'%')", name)
	}
}

// Paginate 分页
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

// GetBrandName GetById 获取品牌名称
func (b *Brand) GetBrandName() error {
	return inits.DB.Where("brand_name like ?", "%"+b.BrandName+"%").Limit(1).Find(&b).Error
}

// BrandDetail 获取品牌详情
func (b *Brand) BrandDetail() error {
	return inits.DB.Find(&b).Error
}
